<?php

namespace app\controllers;

use app\models\User;
use app\service\DanmuService;
use app\service\MemberRecordService;
use app\service\StatService;
use BaseComponents\base\BaseController;

class GAdminController extends BaseController
{

    public $layout = false;

    /**
     * 登录
     * @return string|\yii\web\Response
     */
    public function actionLogin()
    {
        if (!$this->isGuest()) {
            return $this->redirect('/g-admin/home');
        } else {
            return $this->render('login');
        }
    }

    /**
     * 登录逻辑
     * @param $username
     * @param $password
     * @return array
     */
    public function apiLoginHandle($username, $password)
    {
        if ($this->isGuest()) {
            User::login($username, $password);
        }

        return array('href' => '/g-admin/home');
    }

    /**
     * 默认控制器跳转到首页
     * @return $this
     */
    public function actionIndex()
    {
        return $this->redirect('/g-admin/home');
    }

    /**
     * 首页
     * @return $this|string
     */
    public function actionHome()
    {
        if ($this->isGuest()) {
            return $this->redirect('/g-admin/login');
        }

        $homeStatArray = (new StatService())->getHomeStat();

        $liveSwitch = \Yii::$app->get('redis')->get('liveSwitch');

        $lotterySwitch = \Yii::$app->get('redis')->get('lotterySwitch');

        $params = array(
            'homeStatArray' => $homeStatArray,
            'liveSwitch' => !!$liveSwitch,
            'lotterySwitch' => !!$lotterySwitch,
        );

        return $this->render('home', $params);
    }

    /**
     * 直播开关
     * @param $switch
     */
    public function apiLiveSwitch($switch)
    {
        \Yii::$app->get('redis')->set('liveSwitch', $switch == 'true');
    }

    /**
     * 抽奖开关
     * @param $switch
     */
    public function apiLotterySwitch($switch)
    {
        \Yii::$app->get('redis')->set('lotterySwitch', $switch == 'true');
    }

    /**
     * 弹幕列表
     * @return $this|string
     */
    public function actionDanmu()
    {
        if ($this->isGuest()) {
            return $this->redirect('/g-admin/login');
        }

        $page = \Yii::$app->request->get('page', 1);
        $pageSize = 20;

        $params = (new DanmuService())->getPageParams($page, $pageSize);

        return $this->render('danmu', $params);
    }

    /**
     * 抽奖登记记录
     * @return $this|string
     */
    public function actionRecord()
    {
        if ($this->isGuest()) {
            return $this->redirect('/g-admin/login');
        }

        $page = \Yii::$app->request->get('page', 1);
        $pageSize = 20;

        $params = (new MemberRecordService())->getPageParams($page, $pageSize);

        return $this->render('record', $params);
    }

    /**
     * 退出登录
     * @return $this
     */
    public function actionLogout()
    {
        User::getUser()->logout();

        return $this->redirect('/g-admin/login');
    }

    /**
     * 获取用户登录信息
     * @return bool
     */
    private function isGuest()
    {
        return User::getUser()->isGuest;
    }

}